// pages/home/home.js
const api = require('../../util')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: app.globalData.statusBarHeight,
        disabletype: app.globalData.disabletype,
        first_login: app.globalData.first_login,
        is_bind_phone: '',
        miniProgramtype: app.globalData.miniProgramtype,

        current: 0, //当前所在页面的 index
        indicatorDots: false, //是否显示面板指示点
        autoplay: true, //是否自动切换
        interval: 5000, //自动切换时间间隔
        duration: 800, //滑动动画时长
        circular: true, //是否采用衔接滑动

        inpval: '',
        videolists: [],
        listsarr: [],

        completed: '',
        incomplete: '',
        finderusername: '',
        canIUse: wx.canIUse('channel-video'),

        zz: true,
        showzz: false
    },
    getVideoId() {
        var that = this
        api.http('/common/getVideoId', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                finderusername: res.data.finder_user_name
            })
          }
        })
    },
    // 热门数据
    popularArticle() {
        var that = this
        api.http('/index/getPopularArticle', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                listsarr: res.data
            })
          }
        })
    },
    // 热门视频
    popularSelectedVideos() {
        var that = this
        api.http('/index/popularSelectedVideos', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                videolists: res.data
            })
          }
        })
    },
    // 获取是否存在打卡提醒
    normalQuantity() {
        var that = this
        api.http('/MedicationReminder/normalQuantity', 'POST', false, {
        }, function (res) {
          if(res.data.count !== 0) {
            // 获取用药完成次数
            that.CheckedDetails()
          } else {
            that.setData({
                completed: '',
                incomplete: ''
            })
          }
        })
    },
    // 获取用药完成次数
    CheckedDetails() {
        var that = this
        api.http('/index/todayMedication', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                completed: res.data.completed,
                incomplete: res.data.incomplete,
            })
          }
        })
    },
    inputHandler(e) {
        this.setData({
            inpval: e.detail.value
        })
    },
    // 输入框回车触发
    confirmTap() {
        if(!api.disable()) {
            return
        }
        var that = this
        wx.navigateTo({
            url: '/pages/sousuo/sousuo?inptext='+that.data.inpval
        })
    },
    // 跳转精选视频
    tofeaturedvideos() {
        wx.navigateTo({
            url: '/pages/featuredvideos/featuredvideos'
        })
    },
    // 在线复诊
    tofollowup() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/followup/followup'
        }) 
    },
    // 用药管理
    tomedicationmanage() {
        if(!api.disable()) {
            return
        }
        wx.navigateTo({
            url: '/pages/medicationmanage/medicationmanage'
        })
    },
    // 点击观看
    channelfun() {
        var that = this
        api.http('/UserIntegral/AddIntegral', 'POST', false, {
            type: 4
        }, function (res) {
        })
    },
    // 视频跳转
    goAdsBuy(item){
        // var that = this;
        // wx.openChannelsActivity({
        //     finderUserName: that.data.finderusername,
        //     feedId: item.feedId,
        //     success (res) {
        //         that.ifCatAds =true;
        //     },
        //     fail(res) {
        //         console.log(res)
        //     },
        //     complete(res) {
        //         setTimeout(function(){
                    
        //         },500)
        //     }
        // });
    },
    // 跳转推荐详情
    toarticledetails(e) {
        wx.navigateTo({
            url: '/pages/articledetails/articledetails?id='+e.currentTarget.dataset.id
        })
    },
    clockin() {
        if(!api.disable()) {
            return
        }
        if (this.data.completed === '') {
            wx.showModal({
                title: '提示',
                content: '未添加用药提醒，是否前往添加？',
                success: function (sm) {
                  if (sm.confirm) {
                    wx.navigateTo({
                      url: '/pages/remind/remind',
                    })
                  } else if (sm.cancel) {
                  }
                }
            })
        } else {
            wx.switchTab({
                url: '/pages/clockin/clockin'
            }, () => {
            })
        }
    },
    // 跳转帮帮圈
    togangcircle() {
        wx.switchTab({
          url: '/pages/gangcircle/gangcircle',
        })
    },
    // 跳转测评
    toevaluation() {
        var that = this
        wx.navigateTo({
            url: '/pages/evaluation/evaluation',
            success: function () {
                that.setData({
                    zz: false
                })
            },
        });
    },
    // 关闭遮罩
    gbzz() {
      this.setData({
          zz: false
      })  
    },
    // 小程序客服
    handleContact (e) {
        console.log(e.detail.path)
        console.log(e.detail.query)
    },
    // 微信客服
    // btnkf() {
    //     wx.openCustomerServiceChat({
    //         extInfo: {url: ''},
    //         corpId: '',
    //         success(res) {}
    //     })
    // },
    // 监听disabletype
    watchdisabletype: function (name,value) {
        if (value === true) {
           this.setData({
                disabletype: true,
           });
        } else {
            this.setData({
                disabletype: false,
            });
        }
    },
    // 监听first_login
    watchfirst_login: function (name,value) {
        this.setData({
            first_login: value
        });
    },
    getPhoneNumber (e) {
        var that = this
        if (e.detail.errMsg != 'getPhoneNumber:fail no permission'&&e.detail.errMsg != 'getPhoneNumber:fail user deny') {
            console.log(e)
            api.http('/login/bindPhone', 'POST', false, {
                code: e.detail.code
            }, function (res) {
              if(res.code == 200) {
                 that.setData({
                    is_bind_phone: 1
                })
                wx.setStorageSync(app.globalData.miniProgramtype+'is_bind_phone',1)
                wx.showToast({
                    title: res.msg,
                    icon: 'success',
                    duration: 1000
                })
              } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000
                })
              }
            })
        }
    },
    // 是否展示弹框
    is_evaluation() {
        var that = this
        api.http('/index/is_evaluation', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            if(res.data.is_evaluation === 1) {
                that.setData({
                    showzz: true
                })
            } else {
                that.setData({
                    showzz: false
                })
            }
          }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 监听
        getApp().watch('disabletype',this.watchdisabletype);
        getApp().watch('first_login',this.watchfirst_login);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                select: 0
            })
        }
        this.setData({
            inpval: ''
        })
        this.popularSelectedVideos()
        this.popularArticle()
        var that = this
        if (wx.getStorageSync(app.globalData.miniProgramtype+'access_token')=='') {
            app.wxlogin().then(()=>{
                that.normalQuantity()
                that.is_evaluation()
                that.getVideoId()
                that.setData({
                    is_bind_phone: wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone')
                })
            })
        } else {
            that.setData({
                is_bind_phone: wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone')
            })
            that.normalQuantity()
            that.is_evaluation()
            that.getVideoId()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    // 分享到朋友圈
    onShareTimeline(){
        return {
        //   title: '',
        // query: 'id=' + this.id,
        // imageUrl: ''
        }
    }
})